import de.fhpotsdam.unfolding.mapdisplay.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.marker.*;
import de.fhpotsdam.unfolding.tiles.*;
import de.fhpotsdam.unfolding.interactions.*;
import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.core.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.events.*;
import de.fhpotsdam.utils.*;
import de.fhpotsdam.unfolding.providers.*;

import processing.core.*;
import java.io.*;

class Route {
    public static final float epsilon = 0.000001f;
    public static final float IMPOSSIBLE = 1000000;
    public String routeId;
    public float[][] coordinates;
    public String routeName;
    public String routeColor;
    public float[][] screenPositions;
    public static de.fhpotsdam.unfolding.Map map;

    private float topleft[];
    private float width;
    private float height;
    
    public Route(de.fhpotsdam.unfolding.Map m,
                 String routeid, 
                 String name, 
                 float[][] coord,
                 float[] tl,
                 float w,
                 float h
                 )
    {
        map = m;
        routeId = routeid;
        routeName = new String(name);
        routeColor = "#000000";
        coordinates = coord;
        topleft = new float[2];
        topleft[0] = tl[0];
        topleft[1] = tl[1];
        width = w;
        height = h;
    }
    
    public Route(de.fhpotsdam.unfolding.Map m, String routeid, String name, float[][] coord)
    {
        map = m;
        routeId = routeid;
        routeName = new String(name);
        routeColor = "#000000";
        coordinates = coord;
    }
    
    public Route(de.fhpotsdam.unfolding.Map m, String routeid, String name, float[][] coord, String col)
    {
        map = m;
        routeId = routeid;
        routeName = new String(name);
        routeColor = col;
        coordinates = coord;
    }
    
    public float[][] simplifyCoordinates(float[][] cood)
    {
        int p1 = 0;
        int p2 = 1;
        int length = 0;
        for (int i=2; i<cood.length; ++i) {
            int pc = i;
            if (((cood[pc][1] - cood[p2][1]) * (cood[p2][0] - cood[p1][0]) - 
                    (cood[p2][1] - cood[p1][1]) * (cood[pc][0] - cood[p2][0])) 
                    < epsilon) {
                cood[p2][0] = cood[p2][1] = IMPOSSIBLE;
            } else p1 = p2;
            p2 = pc;
        }
        
        float[][] ret = new float[cood.length][2];
        for (int i=0; i<cood.length; ++i) {
            if (cood[i][0] == IMPOSSIBLE) continue;
            ret[length][0] = cood[i][0];
            ret[length][1] = cood[i][1];
            ++length;
        }
        float[][] ret1 = new float[length][2];
        for (int i=0; i<length; ++i) {
            ret1[i][0] = ret[i][0];
            ret1[i][1] = ret[i][1];
        }
        screenPositions = new float[length][2];
        return ret1;
    }
    
    public void convertCoordinatesToPositions(float[][] cood)
    {
        //if (screenPositions == null) 
        //    screenPositions = new float[cood.length][2];
            
        Location loc = new Location(cood[0][0], cood[0][1]);
        float[] xy = map.getScreenPositionFromLocation(loc);
        screenPositions[0][0] = xy[0];
        screenPositions[0][1] = xy[1];
        for (int i=1; i<cood.length; ++i) {
            loc.setLat(cood[i][0]);
            loc.setLon(cood[i][1]);
            xy = map.getScreenPositionFromLocation(loc);
            screenPositions[i][0] = xy[0];
            screenPositions[i][1] = xy[1];
        }
    }
    
    public boolean inSideMap(float px, float py)
    {
        if (px < topleft[0] || py < topleft[1]
            || px > topleft[0] + width || py > topleft[1] + height)
            return false;
        return true;
    }
    
    public de.fhpotsdam.unfolding.Map setMap(de.fhpotsdam.unfolding.Map m)
    {
        de.fhpotsdam.unfolding.Map ret = map;
        map = m;
        return ret;
    }
    
    public float[] getScreenPositionFromLocation(float[] position)
    {
        return map.getScreenPositionFromLocation(new Location(position[0], position[1]));
    }
    
    public float[] getLocationFromScreenPosition(float[] position)
    {
        Location loc = map.getLocationFromScreenPosition(position[0], position[1]);
        float[] ret = new float[2];
        ret[0] = loc.getLat();
        ret[1] = loc.getLon();
        return ret;
    }
    
    public void draw()
    {
        //fill(204, 155, 0, 20);
        
    }
}
